package com.learner.ucenter.controller.api;

import com.learner.pojo.user.vo.GroupHistoryResultVo;
import com.learner.pojo.user.vo.GroupMessageResultVo;
import com.learner.pojo.user.vo.HistoryMsgRequestVo;
import com.learner.ucenter.service.GroupMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 群聊消息控制器
 **/
@RequestMapping("/api/ucenter/groupMessage")
@RestController
@Slf4j
public class GroupMessageController {

    @Autowired
    private GroupMessageService groupMessageService;

    /**
     * 获取最近的群消息
     *
     * @param roomId    组id
     * @param pageIndex pageIndex
     * @param pageSize  pageSize
     * @return 结果
     */
    @GetMapping("/getRecentGroupMessages")
    public List<GroupMessageResultVo> getRecentGroupMessages(String roomId, Integer pageIndex, Integer pageSize) {
        log.info("获取最近的群消息，参数roomId:{},page:{},pageSize:{}", roomId, pageIndex, pageSize);
        List<GroupMessageResultVo> recentGroupMessages = groupMessageService.getRecentGroupMessages(roomId, pageIndex, pageSize);
        log.info("获取最近的群消息，请求结果:{}", recentGroupMessages);
        return recentGroupMessages;
    }

    /**
     * 获取群历史消息
     *
     * @param historyMsgRequestVo 组id
     * @return 结果
     */
    @PostMapping("/historyMessages")
    public GroupHistoryResultVo getGroupHistoryMessages(@RequestBody HistoryMsgRequestVo historyMsgRequestVo) {
        GroupHistoryResultVo historyMessages = groupMessageService.getGroupHistoryMessages(historyMsgRequestVo);
        log.info("查询到的群历史消息：{}",historyMessages);
        return historyMessages;
    }

    /**
     * 获取群的最后一条消息
     *
     * @param roomId 组id
     * @return 结果
     */
    @GetMapping("/lastMessage")
    public GroupMessageResultVo getGroupLastMessage(String roomId) {
        GroupMessageResultVo groupLastMessage = groupMessageService.getGroupLastMessage(roomId);
        log.info("获取群{}的最后一条消息:{}",roomId,groupLastMessage);
        return groupLastMessage;
    }
}
